package com.csxy.six.controller;

import com.csxy.six.R;
import com.csxy.six.api.UserClient;
import com.csxy.six.entity.GoodsComment;
import com.csxy.six.entity.vo.GoodsCommentVO;
import com.csxy.six.service.GoodsCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "goods-comments", description = "商品评论管理")
@RestController
@RequestMapping("/market/comments")
public class GoodsCommentController {

    @Autowired
    private GoodsCommentService goodsCommentService;
    @Autowired
    private UserClient userClient;

    @Operation(summary = "根据商品ID查询评论", description = "根据商品ID查询评论")
    @GetMapping("/listByGoods/{goodsId}")
    public R getCommentsByGoodsId(@PathVariable Integer goodsId) {
        List<GoodsComment> list = goodsCommentService.getCommentsByGoodsId(goodsId);
        return R.ok().data("items", list);
    }

    @Operation(summary = "新增商品评论", description = "新增商品评论")
    @PostMapping("/save")
    public R addComment(@Valid @RequestBody GoodsCommentVO vo) {
        if (vo.getGoodsId() == null || vo.getGoodsId() <= 0) {
            return R.error().message("商品ID不能为空");
        }

        if (vo.getParentId() != null) {
            // 检查 parent_id 是否存在
            GoodsComment parentComment = goodsCommentService.getById(vo.getParentId());
            if (parentComment == null) {
                return R.error().message("父评论ID不存在");
            }
        }

        GoodsComment comment = new GoodsComment();
        comment.setGoodsId(vo.getGoodsId());
        comment.setUserId(vo.getUserId());
        comment.setContent(vo.getContent());
        comment.setParentId(vo.getParentId());

        boolean flag = goodsCommentService.save(comment);
        return flag ? R.ok().message("评论提交成功") : R.error().message("评论提交失败");
    }

    @Operation(summary = "根据删除评论", description = "根据删除评论")
    @DeleteMapping("/remove")
    public R removeComment(
            @RequestParam Integer commentId,
            @RequestParam(required = false) Integer goodsId) {
        if (commentId == null || commentId <= 0) {
            return R.error().message("评论ID不能为空");
        }

        GoodsComment comment = goodsCommentService.getById(commentId);
        if (comment == null) {
            return R.error().message("评论不存在");
        }

        // 可选：验证是否属于指定商品
        if (goodsId != null && !comment.getGoodsId().equals(goodsId)) {
            return R.error().message("评论不属于指定商品");
        }

        boolean flag = goodsCommentService.removeById(commentId);
        return flag ? R.ok().message("评论删除成功") : R.error().message("评论删除失败");
    }


    @Operation(summary = "修改商品评论", description = "修改商品评论")
    @PostMapping("/update")
    public R updateComment(@RequestBody GoodsCommentVO vo) {
        if (vo.getCommentId() == null || vo.getCommentId() <= 0) {
            return R.error().message("评论ID不能为空");
        }

        GoodsComment comment = goodsCommentService.getById(vo.getCommentId());
        if (comment == null) {
            return R.error().message("评论不存在");
        }

        // 仅允许修改内容等字段，确保评论ID不变
        comment.setGoodsId(vo.getGoodsId());
        comment.setUserId(vo.getUserId());
        comment.setContent(vo.getContent());
        comment.setParentId(vo.getParentId());

        boolean flag = goodsCommentService.updateById(comment);
        return flag ? R.ok().message("评论修改成功") : R.error().message("评论修改失败");
    }

}
